package com.mx.base.language.script

import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.usermodel.WorkbookFactory
import java.io.File
import java.io.FileOutputStream


/**
 * @author: xiao.ma
 * Date: 2022/3/28 17:28
 * Desc:
 */

object TestScript {

    private fun readBaseLanguageFromExcel(excelLanguageFile: File): MutableMap<String, String> {
        val languageMap = mutableMapOf<String, String>()
        val workbook = WorkbookFactory.create(excelLanguageFile)
        val sheet = workbook.getSheet("Sheet1")
        val header = sheet.getRow(0)
        if (header != null) {
            val firstRowIndex = sheet.firstRowNum + 1
            val lastRowIndex = sheet.lastRowNum
            for (rowIndex in firstRowIndex..lastRowIndex) {
                val row = sheet.getRow(rowIndex)
                if (row != null) {
                    val key = row.getCell(0)?.stringCellValue
                    if (!key.isNullOrBlank()) {
                        val languageValue = row.getCell(1)?.stringCellValue
                        if (!languageValue.isNullOrEmpty()) {
                            languageMap[key] = languageValue
                        }
                    }
                }

            }
        }
        workbook.close()
        return languageMap
    }

    fun translation() {
        val translationMap = ExcelHelper.readLanguageFromExcel(
            "D:\\Language\\strings.xlsx",
            mutableListOf()
        )
        File("D:\\Language\\lg").listFiles()?.forEach {
            if (it.isFile) {
                val map = readBaseLanguageFromExcel(it)
                fillDataToExcelByModule("m_${it.name}", translationMap, map)
            }
        }
    }

    private fun fillDataToExcelByModule(
        name: String,
        translationMap: MutableMap<String, MutableMap<String, String>>,
        baseXmlMap: Map<String, String>
    ) {
        val workbook = WorkbookFactory.create(true)
        val sheet = workbook.createSheet("Sheet1")
        var rowNum = 0
        val header = sheet.createRow(rowNum++)
        val header2 = sheet.createRow(rowNum++)
        var columnNum = 0

        //填充头部数据
        header.createCell(0).apply {
            setCellValue("key")
        }
        header2.createCell(0).apply {
            setCellValue("")
        }

        header.createCell(1).apply {
            setCellValue("values-zh-rCN")
        }
        header2.createCell(1).apply {
            setCellValue(Constants.languageKeyMap["values-zh-rCN"] ?: "")
        }
        columnNum = 2
        //填充头部数据
        translationMap.forEach {
            header.createCell(columnNum).apply {
                setCellValue(it.key)
            }
            header2.createCell(columnNum++).apply {
                setCellValue(Constants.languageKeyMap[it.key] ?: "")
            }
        }

        baseXmlMap.forEach { baseMap ->
            columnNum = 0
            val row = sheet.createRow(rowNum++)
            row.createCell(columnNum++).apply {
                setCellValue(baseMap.key)
            }
            row.createCell(columnNum++).apply {
                setCellValue(baseMap.value)
            }
            translationMap.forEach { lMap ->
                row.createCell(columnNum++).apply {
                    setCellValue(lMap.value[baseMap.value])
                }
            }
        }
        val excelFile = File("D:\\Language\\lg\\t\\$name").apply {
            if (exists()) {
                delete()
            }
        }
        FileOutputStream(excelFile).use { fos ->
            workbook.write(fos)
        }
    }
}

fun main() {
    TestScript.translation()
}

